package com.guofen.controller.hsmpy;

import com.guofen.pojo.HsmMiddleWorm;
import com.guofen.service.hsmpy.MiddleWormBreedingMaterialService;
import com.guofen.util.result.Result;
import com.guofen.util.result.ResultGenerator;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@RequestMapping("/hsmpy/middleWormBreedingMaterial")
@Controller
@Api(tags = "MiddleWormBreedingMaterialController", description = "3-3 中虫培育出料记录表")
public class MiddleWormBreedingMaterialController {

    @Autowired
    private MiddleWormBreedingMaterialService middleWormBreedingMaterialService;

    @ApiOperation("增加记录")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public Result add(@ApiParam(name = "id", value = "id", required = true) @RequestParam String id,
                      @ApiParam(name = "big_bug_breed_end_time", value = "中虫培育结束日期", required = true) @RequestParam String big_bug_breed_end_time,
//                      @ApiParam(name = "after_big_weight", value = "中虫培育盒重", required = true) @RequestParam String after_big_weight,
                      @ApiParam(name = "go_where", value = "去向", required = true) @RequestParam String go_where){
        Integer Id = Integer.parseInt(id);
//        Double after_big_w = Double.parseDouble(after_big_weight);
        if (middleWormBreedingMaterialService.add(Id, big_bug_breed_end_time, go_where))
            return ResultGenerator.genSuccessResult();
        else
            return ResultGenerator.genFailResult("保存失败");
    }

    @ApiOperation("返回当天最新一条数据")
    @RequestMapping(value = "/getlatest", method = RequestMethod.GET)
    @ResponseBody
    public Result getlatest(){
        return ResultGenerator.genSuccessResult(middleWormBreedingMaterialService.getlatest());
    }

    @ApiOperation("返回还未填写过3-3内容的n条数据")
    @RequestMapping(value = "/getlist", method = RequestMethod.GET)
    @ResponseBody
    public Result getlist(){
        return ResultGenerator.genSuccessResult(middleWormBreedingMaterialService.getlist());
    }

    @ApiOperation("删除记录")
    @RequestMapping(value = "/delete",method = RequestMethod.POST)
    @ResponseBody
    public Result delete(@ApiParam(name="id",value="ID",required=true) @RequestParam Integer id) {
        return ResultGenerator.genSuccessResult(middleWormBreedingMaterialService.delete(id));
    }

    @ApiOperation("修改记录")
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    @ResponseBody
    public Result update(@ApiParam(name="id",value="ID",required=true) @RequestParam Integer id,
                         @ApiParam(name="batch",value="批次",required=true) @RequestParam String batch,
                         @ApiParam(name="wormGroup",value="组别",required=true) @RequestParam String wormGroup,
                         @ApiParam(name="smallBugBreedBoxNumber",value="孵化盒号",required=true) @RequestParam String smallBugBreedBoxNumber,
                         @ApiParam(name="bigBugBreedBoxNumber",value="中虫培育盒号",required=true) @RequestParam String bigBugBreedBoxNumber,
                         @ApiParam(name="isQualified",value="抽检",required=true) @RequestParam String isQualified,
                         @ApiParam(name="inspectionRemarks",value="备注",required=true) @RequestParam String inspectionRemarks) {
        HsmMiddleWorm record = new HsmMiddleWorm();
        record.setId(id);
        record.setBatch(batch);
        record.setWormGroup(wormGroup);
        record.setSmallBugBreedBoxNumber(smallBugBreedBoxNumber);
        record.setBigBugBreedBoxNumber(bigBugBreedBoxNumber);
        record.setIsQualified(isQualified);
        record.setInspectionRemarks(inspectionRemarks);
        return ResultGenerator.genSuccessResult(middleWormBreedingMaterialService.update(record));
    }

    @ApiOperation("查找记录")
    @RequestMapping(value = "/select",method = RequestMethod.POST)
    @ResponseBody
    public Result select(@ApiParam(name="pageSize",value="页容量",required=true) @RequestParam Integer pageSize,
                         @ApiParam(name="currentPage",value="当前页",required=true) @RequestParam Integer currentPage,
                         @ApiParam(name="startTime",value="开始时间") Long startTime,
                         @ApiParam(name="endTime",value="截止时间") Long endTime) {
        if (startTime == null || endTime == null)
            return ResultGenerator.genSuccessResult(middleWormBreedingMaterialService.get(pageSize, currentPage));
        else
            return ResultGenerator.genSuccessResult(middleWormBreedingMaterialService.getByTime(pageSize, currentPage, startTime, endTime));
    }
}
